************************************
* Construct measures for near-misses
************************************


gen hb1=total_prev_hard_brake
gen hb2=total_real_hard_brake_1
gen hb3=total_hard_brake

gen hb4=hb1+total_hard_turn
gen hb5=hb2+total_hard_turn
gen hb6=hb3+total_hard_turn


forvalues i=1(1)6{
	gen hb`i'_dummy=1 if hb`i'>0
	replace hb`i'_dummy=0 if hb`i'_dummy==.
}


forvalues i=7(1)9{
	loc j=`i'-6
	display `j'

	gen hb`i'=1 if hb`j'>0 & total_hard_turn>0
	replace hb`i'=0 if hb`i'==.
}


sum hb1-hb9

***********************
* Gen fisrt-diff for NM
***********************


forvalues i=1(1)6{
	gen diff_hb`i'=hb`i'-l.hb`i'
}

forvalues i=1(1)6{
	gen diff_hb`i'_dummy=hb`i'_dummy-l.hb`i'_dummy
}

forvalues i=7(1)9{
	gen diff_hb`i'=hb`i'-l.hb`i'
}


order diff_hb1_dummy-diff_hb3_dummy diff_hb7-diff_hb9, after(hb9)
// diff_hb1_dummy-diff_hb9, reported in the main tables
// diff_hb1-diff_hb6_dummy, reporeted in the robustness checks

**********************************************
* Gen fisrt-diff for risky behavior and others
**********************************************


gen diff_agg_acc=total_agg_acc-l.total_agg_acc

gen diff_phone_use=total_phone_use-l.total_phone_use

gen diff_traffic_jam=total_traffic_jam-l.total_traffic_jam
gen diff_traffic_time=total_traffic_sec-l.total_traffic_sec
gen diff_distance=distance-l.distance
gen diff_duration=duration-l.duration
gen diff_speed=speed-l.speed
gen diff_speed2=speed2-l.speed2
gen diff_drive_at_night=drive_at_night-l.drive_at_night
gen diff_night_prop=night_duration_prop-l.night_duration_prop
gen diff_night_duration=total_night_duration-l.total_night_duration
gen diff_weekend=weekend-l.weekend
gen diff_rush_hour=rush_hour-l.rush_hour

gen diff_control_score=control_score-l.control_score
gen diff_cautious_score=cautious_score-l.cautious_score
gen diff_focused_score=focused_score-l.focused_score
gen diff_drive_score=drive_score-l.drive_score
gen diff_gap_time=gap_time-l.gap_time

gen diff_highway=highway-l.highway

** gen first_difference for weather variables, 
gen diff_sunny=sunny-l.sunny
gen diff_rain=rain-l.rain
gen diff_snow=snow-l.snow
gen diff_rain_storm=rain_storm-l.rain_storm
gen diff_cloudy=cloudy-l.cloudy

gen diff_high_temper=high_temper-l.high_temper
gen diff_low_temper=low_temper-l.low_temper
